import java.util.HashMap;
import java.util.Map;

public class CartService {
    private Map<Integer, Cart> userCarts = new HashMap<>();

    public void addToCart(Integer userId, Integer productId, Integer quantity) {
        Cart cart = userCarts.getOrDefault(userId, new Cart(userId, new HashMap<>()));
        cart.getItems().merge(productId, quantity, Integer::sum);
        userCarts.put(userId, cart);
    }

    public Cart getCart(Integer userId) {
        return userCarts.getOrDefault(userId, new Cart(userId, new HashMap<>()));
    }

    public void clearCart(Integer userId) {
        userCarts.remove(userId);
    }
}